﻿$(function () {
    var serializer = new JSONSerializer();
    //修改时绑定页面
    var url = new Url();
    var $Id = url.GetQueryString("id");
    var $css = url.GetQueryString("css");
    if ($Id && !isNaN($Id)) {//编辑模式，加载并绑定角色信息
        $.getJSON("../Handler/RoleHandler.ashx", { Id: $Id, type: 4, _: Math.random() },
            function (result) {
                if (result.Status) {
                    $("#roleName").val(result.Data.RoleName);
                    $("#RoleType").val(result.Data.RoleType);
                    if (result.Data.menus) {
                        var nameList = new Array();
                        var idList = new Array();
                        $(result.Data.menus).each(function (index, item) {
                            nameList.push(item.MenuName);
                            idList.push(item.MenuId);
                        });
                        var menus = nameList.join(',');
                        $("#menuRight").val(menus.length >= 14 ? menus.substr(0, 14) + "..." : menus);
                        $("#hdMenus").val(idList.join(","));
                        $("#getMenus").html("修改菜单");
                    }
                    else {
                        $("#menuRight").text("您还未选择任何菜单");
                        $("#getMenus").html("请选择");
                    }
                    if (result.Data.pages) {
                        var nameList = new Array();
                        var idList = new Array();
                        $(result.Data.pages).each(function (index, item) {
                            nameList.push(item.PageName);
                            idList.push(item.PageId);
                        });
                        var pages = nameList.join(',');
                        $("#pageRight").val(pages.length >= 14 ? pages.substr(0, 14) + "..." : pages);
                        $("#hdPages").val(idList.join(","));
                        $("#getPages").html("修改页面");
                    }
                    else {
                        $("#pageRight").text("您还未选择任何页面");
                        $("#getPages").html("请选择");
                    }
                    if (result.Data.funcs) {
                        var nameList = new Array();
                        var idList = new Array();
                        $(result.Data.funcs).each(function (index, item) {
                            nameList.push(item.PageName);
                            idList.push(item.PageId);
                        });
                        var pages = nameList.join(',');
                        $("#funcRight").val(pages.length >= 14 ? pages.substr(0, 14) + "..." : pages);
                        $("#hdFuncs").val(idList.join(","));
                        $("#getFuncs").html("修改功能");
                    }
                    else {
                        $("#funcRight").text("您还未选择任何操作");
                        $("#getFuncs").html("请选择");
                    }
                } else {
                    alert(result.Message);
                }
            }
        );
    }
    //获取菜单
    $("#getMenus").click(function () {
        if ($("#menuTree ul li").length <= 0) {
            $.getJSON("../Handler/MenuHandler.ashx", { type: 6, _: Math.random() },
                function (result) {
                    if (result.Status) {
                        $('#menuTree').jstree({
                            "core": { "data": result.Data },
                            "checkbox": {
                                "keep_selected_style": false
                            },
                            "plugins": ["checkbox"]
                        });
                        $('#menuTree').on("changed.jstree", function (e, data) {
                            var idList = new Array();
                            var nameList = new Array();
                            for (var i = 0; i < data.selected.length; i++) {
                                if (idList.indexOf(data.selected[i]) < 0) {
                                    idList.push(data.selected[i]);
                                    nameList.push(data.instance.get_node(data.selected[i]).text);
                                }
                                if (data.instance.is_leaf(data.selected[i])) {
                                    var parent = data.instance.get_parent(data.selected[i]);
                                    if (idList.indexOf(parent) < 0 && parent != "#") {
                                        idList.push(parent);
                                        nameList.push(data.instance.get_node(parent).text);
                                    }
                                }
                            }
                            var menus = nameList.join(',');
                            $("#menuRight").val(menus.length >= 10 ? menus.substr(0, 10) + "..." : menus);
                            $("#hdMenus").val(idList.join(','));
                        });
                        $('#menuTree').on("ready.jstree", function (e, data) {
                            var idstr = $("#hdMenus").val();
                            var array = idstr.split(",");//分割字符串成数组
                            for (var i = 0; i < array.length; i++) {
                                if ($.jstree.reference('#menuTree').is_leaf(array[i])) {
                                    $.jstree.reference('#menuTree').select_node(array[i]);
                                }
                            }
                        });
                    }
                    else {
                        alert(result.Message);
                    }
                }
            );
        }
    });
    //获取页面权限
    $("#getPages").click(function () {
        if ($("#pageTree ul li").length <= 0) {
            $.getJSON("../Handler/PageRightHandler.ashx", { type: 6, pageType: true, _: Math.random() }, function (result) {
                if (result.Status) {
                    $("#pageTree").jstree({
                        "core": { "data": result.Data },
                        "checkbox": {
                            "keep_selected_style": false
                        },
                        "plugins": ["checkbox"]
                    });
                    $('#pageTree').on("changed.jstree", function (e, data) {
                        var idList = new Array();
                        var nameList = new Array();
                        for (var i = 0; i < data.selected.length; i++) {
                            if (data.instance.is_leaf(data.selected[i]) && !isNaN(data.selected[i])) {
                                idList.push(data.selected[i]);
                                nameList.push(data.instance.get_node(data.selected[i]).text);
                            }
                        }
                        var menus = nameList.join(',');
                        $("#pageRight").val(menus.length >= 10 ? menus.substr(0, 10) + "..." : menus);
                        $("#hdPages").val(idList.join(','));
                    });
                    $('#pageTree').on("ready.jstree", function (e, data) {
                        var idstr = $("#hdPages").val();
                        var array = idstr.split(",");//分割字符串成数组
                        for (var i = 0; i < array.length; i++) {
                            $.jstree.reference('#pageTree').select_node(array[i]);
                        }
                    });
                }
                else {
                    alert(result.Message);
                }
            });
        }
    });
    //获取功能权限
    $("#getFuncs").click(function () {
        if ($("#funcTree ul li").length <= 0) {
            $.getJSON("../Handler/PageRightHandler.ashx", { type: 6, pageType: false, _: Math.random() }, function (result) {
                if (result.Status) {
                    $("#funcTree").jstree({
                        "core": { "data": result.Data },
                        "checkbox": {
                            "keep_selected_style": false
                        },
                        "plugins": ["checkbox"]
                    });
                    $('#funcTree').on("changed.jstree", function (e, data) {
                        var idList = new Array();
                        var nameList = new Array();
                        for (var i = 0; i < data.selected.length; i++) {
                            if (data.instance.is_leaf(data.selected[i]) && !isNaN(data.selected[i])) {
                                idList.push(data.selected[i]);
                                nameList.push(data.instance.get_node(data.selected[i]).text);
                            }
                        }
                        var menus = nameList.join(',');
                        $("#funcRight").val(menus.length >= 10 ? menus.substr(0, 10) + "..." : menus);
                        $("#hdFuncs").val(idList.join(','));
                    });
                    $('#funcTree').on("ready.jstree", function (e, data) {
                        var idstr = $("#hdFuncs").val();
                        var array = idstr.split(",");//分割字符串成数组
                        for (var i = 0; i < array.length; i++) {
                            $.jstree.reference('#funcTree').select_node(array[i]);
                        }
                    });
                }
                else {
                    alert(result.Message);
                }
            });
        }
    });
    //编辑后保存
    $(".btnSave").bind("click", function () {
        var $formData = serializer.Serialize($("#formArea")[0]);
        if (!$formData.RoleName) {
            $("#roleName").focus();
            $(".roleNameError").text("角色名称不能为空");
            return;
        }
        if ($formData.RoleType == "-1") {
            $(".RoleError").text("请选择角色类型");
            return;
        }
        $formData.type = $Id ? 2 : 1;
        $formData.Id = $Id;
        $formData._ = Math.random();
        if ($formData.Pages) {
            if (($("#hdFuncs").val())) {
                $formData.Pages += "," + $("#hdFuncs").val();
            }
        }
        else {
            if (($("#hdFuncs").val())) {
                $formData.Pages = $("#hdFuncs").val();
            }
        }
        $.post("../Handler/RoleHandler.ashx", $formData,
            function (result) {
                alert(result.Message);
                if (result.Status) {
                    location.href = "RoleList.aspx?css=" + $css;
                }
            }, "json"
        );
    });
    $(".cancel").attr("href", "RoleList.aspx?css=" + $css);

    $("#Role,#roleName").change(function () {
        $(this).siblings(".error").empty();
    });
});